package appController

import (
	"CMETO/models"
	"CMETO/utils"
	"CMETO/utils/jwt"
	"net/http"

	"github.com/gin-gonic/gin"
)

// app部分的接口
type AccountController struct{}

// @Summary 商品分类查询（树状所有）
// @Description 树状查询商品分类
// @Tags APP管理
// @Accept json
// @Produce json
// @param password query string false "密码"
// @Success 200 {object} models.ResultCommon[[]models.User] "密码正确"
// @Success 401 {object} models.ResultCommon[string] "密码错误"
// @Router /app/account/check [get]
// @Security JWT
func (r AccountController) CheckAccount(ctx *gin.Context) {
	password := ctx.DefaultQuery("password", "")
	var user []models.User
	password = utils.Sha256(password)
	token, _ := ctx.Get("token")
	id := &token.(*jwt.MyClaims).User.Id
	models.DB.Model(&user).Where("password = ? AND id = ?", password, id).Find(&user)
	if len(user) == 0 {
		ctx.JSON(http.StatusOK, models.ResultCommon[string]{
			Msg:    "密码错误",
			Code:   "400",
			Result: "",
		})
	} else {
		ctx.JSON(http.StatusOK, models.ResultCommon[[]models.User]{
			Msg:    "密码正确",
			Code:   "200",
			Result: user,
		})
	}
}
